package com.example.snowliwork;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class PlanetGridAdapter extends BaseAdapter {

    //获取上下文环境
    private Context context;
    //item布局资源
    private int layoutId;
    //数据源
    private List<Shop> shop;


    public PlanetGridAdapter() {

    }
    public PlanetGridAdapter(Context nContext, int layoutId, List<Shop> shop) {
        this.context = nContext;
        this.layoutId = layoutId;
        this.shop = shop;
    }


//返回数据源中的元素个数
    @Override
    public int getCount() {
        return shop.size();
    }

    //返回指定位置的元素
    @Override
    public Object getItem(int position) {
        return shop.get(position);
    }


    //返回指定位置
    @Override
    public long getItemId(int position) {
        return position;
    }


    //想页面中添加数据
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //1.获取布局文件对象
        View view = LayoutInflater.from(context).inflate(layoutId,null);
        //2.获取布局文件中控件的对象
        ImageView ivImage = view.findViewById(R.id.if_image);
        TextView tvDetail = view.findViewById(R.id.if_detail);
        TextView tvSafe = view.findViewById(R.id.if_safe);
        TextView tvPrice = view.findViewById(R.id.if_price);
        //3.获取数据源中当前位置的元素
        Shop sho = shop.get(position);
        //4.把数据源中的数据显示到对应控件中去
        ivImage.setImageResource(sho.getsImage());
        tvDetail.setText(sho.getsDetail());
        tvSafe.setText(sho.getsSafe());
        tvPrice.setText(sho.getsPrice());

        return view;
    }
}















